package com.xiaoliang.service.impl;

import com.xiaoliang.dao.UserDao;
import com.xiaoliang.dao.impl.UserDaoImpl;
import com.xiaoliang.entry.User;
import com.xiaoliang.service.UserService;
import com.xiaoliang.tools.JDBCUtils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author XIAOLIANG
 * @ClassName UserServiceImpl.java
 * @Description TODO
 * @createTime 2021年02月25日 19:00:00
 */
public class UserServiceImpl implements UserService {

	UserDao userDao = new UserDaoImpl();

	@Override
	public boolean login(User user) {
		Connection connection = JDBCUtils.getConnection();;
		Integer i;
		boolean isExist = false;
		try {
			connection.setAutoCommit(false);
			isExist = userDao.selectIsExist(user);
			connection.commit();

		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
			e.printStackTrace();
			throw  new RuntimeException(e);

		} finally {
			JDBCUtils.close(connection,null);
		}
		return isExist;

	}

	@Override
	public boolean register(User user) {
		Connection connection = JDBCUtils.getConnection();;
		Integer i = 0;
		try {
			connection.setAutoCommit(false);
			i = userDao.insert(user);
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
			e.printStackTrace();
			throw  new RuntimeException(e);

		} finally {
			JDBCUtils.close(connection,null);
		}
		return i>0;
	}

}
